<?php

namespace app\common\model;

use think\Model;


class TeacherJob extends Model
{

    

    

    // 表名
    protected $name = 'teacher_job';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'addr_text'
    ];

    protected static function init()
    {
        self::afterWrite(function ($row) {
            $pk = $row->getPk();
            if(isset($row->workaddr_area)){
                $workaddr_area = $row->workaddr_area;
                if($workaddr_area){
                    $workaddr_area = explode(',', $workaddr_area);
                    $lat = $workaddr_area[2];
                    $lng = $workaddr_area[3];
                    $row->getQuery()->where($pk, $row[$pk])->update(['lat' => $lat, 'lng' => $lng]);
                }
            }
        });
    }

    /**
     * 订单支付成功
     *
     * @param [type] $order
     * @param [type] $notify
     * @return void
     */
    public function paySuccess($order, $notify)
    {
        $_time = time();
        $order->status = 2;
        $order->paytime = $_time;
        $order->transaction_id = $notify['transaction_id'];
        $order->payment_json = $notify['payment_json'];
        $order->pay_type = $notify['pay_type'];
        $order->pay_fee = $notify['pay_fee'];
        $order->save();

        // 支付后监听
//        $params = ['order'=>$order,'type'=>'teacher_job'];
//        \think\Hook::listen('xykeep_order_payed_after', $params);

        return $order;
    }



    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getAddrTextAttr($value, $data){
        $value = $value ? $value : (isset($data['addr']) ? $data['addr'] : '');
        return explode(',',$data['addr']);
    }
}
